<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <TreeTools :tree-node="company" :is-root="true" @addDepts="addDepts" />
        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <TreeTools
            slot-scope="{ data }"
            :tree-node="data"
            @delDepts="getDepartments"
            @addDepts="addDepts"
            @editDepts="editDepts"
          />
        </el-tree>
      </el-card>
    </div>
    <AddDept
      ref="addDept"
      :show-dialog.sync="showDialog"
      :tree-node="node"
      @addDepts="getDepartments"
    />
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'
import AddDept from './components/add-dept' // 引入新增部门组件
export default {
  name: '',
  components: {
    AddDept,
    TreeTools
  },
  props: {},
  data () {
    return {
      departs: [],
      defaultProps: {
        label: 'name'
      },
      company: {},
      node: {},
      loading: false, // 用来控制进度弹层的显示和隐藏
      showDialog: false // 显示窗体
    }
  },

  computed: {},
  watch: {},
  created () {
    this.getDepartments()
  },
  mounted () { },
  methods: {
    async getDepartments () {
      this.loading = true
      const result = await getDepartments()
      this.company = { name: result.companyName, manager: '负责人', id: '' }
      this.departs = tranListToTreeData(result.depts, '') // // 需要将其转化成树形结构
      this.loading = false
    },
    addDepts (node) {
      this.showDialog = true
      this.node = node
    },
    editDepts (node) {
      this.showDialog = true
      this.node = node
      // 我们需要在这个位置 调用子组件的方法
      // 父组件 调用子组件的方法
      this.$refs.addDept.getDepartDetail(this.node.id) // 直接调用子组件中的方法 传入一个id
    }
  }
}
</script>
<style lang='css' scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
